package com.aphrodite.io.sip.net;

import javax.microedition.io.Connector;
import javax.microedition.io.Datagram;
import javax.microedition.io.UDPDatagramConnection;

/**
 * UdpSocket provides a uniform interface to UDP transport protocol, regardless
 * J2SE or J2ME is used.
 */
public final class UdpSocket {

	/** DatagramSocket */
	UDPDatagramConnection socket;

	/** Creates a new UdpSocket */
	public UdpSocket(int port) {

		try {
			socket = (UDPDatagramConnection) Connector.open("datagram://:" + port);
		} catch (java.io.IOException e) {
			e.printStackTrace();
		}
	}

	/** Creates a new UdpSocket */
	public UdpSocket(int port, IpAddress ipaddr) {
		try {
			socket = (UDPDatagramConnection) Connector.open("datagram://:" + port);
		} catch (java.io.IOException e) {
			e.printStackTrace();
		}
	}

	/** Closes this datagram socket. */
	public void close() {
		try {
			socket.close();
		} catch (java.io.IOException e) {
			e.printStackTrace();
		}
	}

	/** Gets the local address to which the socket is bound. */
	public IpAddress getLocalAddress() {
		try {
			return new IpAddress(socket.getLocalAddress());
		} catch (java.io.IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/** Gets the port number on the local host to which this socket is bound. */
	public int getLocalPort() {
		try {
			return socket.getLocalPort();
		} catch (java.io.IOException e) {
			e.printStackTrace();
			return 0;
		}
	}

	/** Gets the socket timeout. */
	public int getSoTimeout() {
		return 0;
	}

	/** Receives a datagram packet from this socket. */
	public void receive(UdpPacket pkt) throws java.io.IOException {
		Datagram dgram = pkt.getDatagram();
		socket.receive(dgram);
		pkt.setDatagram(dgram);
		String url = dgram.getAddress(); // datagram://addr:port
		System.err.println("DEBUG: UdpSocket: received from: " + url);
		int begin = url.indexOf("://");
		begin += 3;
		int end = url.indexOf(":", begin);
		String addr = url.substring(begin, end);
		int port = Integer.parseInt(url.substring(end + 1, url.length()));
		pkt.setIpAddress(new IpAddress(addr));
		pkt.setPort(port);
	}

	/** Sends an UDP packet from this socket. */
	public void send(UdpPacket pkt) throws java.io.IOException {
		Datagram dgram = pkt.getDatagram();
		socket.send(dgram);
		String url = dgram.getAddress(); // datagram://addr:port
		System.err.println("DEBUG: UdpSocket: sent to: " + url);
	}

	/**
	 * Enables/disables socket timeout with the specified timeout, in
	 * milliseconds.
	 */
	public void setSoTimeout(int timeout) {}

	/** Converts this object to a String. */
	public String toString() {
		try {
			return "udp:" + socket.getLocalPort();
		} catch (java.io.IOException e) {
			return "udp:0";
		}
	}

}
